package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//权限服务
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
     private PermissionDao permissionDao;

  //添加权限
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //        mybatis 分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void del(Integer id)  {
        //判断是否与角色关联
        long num = permissionDao.findCountByPermissionId(id);
        if (num>0){
             throw new RuntimeException("当前权限被引用，不能删除");
        }
        permissionDao.del(id);
    }
//回显权限查询  用于编辑时
    @Override
    public Permission FindAll(Integer id) {
        Permission permission = permissionDao.FindAll(id);
        return permission;
    }
//编辑权限
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }
//查询所有权限数据
    @Override
    public List<Permission> findAll() {
        List<Permission> permissionList=permissionDao.findAll();
        return permissionList;
    }
}
